package com.ergwang.dao;

import com.ergwang.Model.Orders;
import com.ergwang.Model.mall.vo.Orders_GoodsInfoVO;
import com.ergwang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author：ergwang
 * @Date: 2020/6/2
 * @Time: 14:46
 */
public class OrderMallDaoImpl implements Order_MallDao {

    // 获取购物车数据的前半部分（订单表）
    public List<Orders> getOrders_OrderInfo(String stateId, String nickname) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Orders> query = null;
        try {
            query = runner.query("select * from orders where stateId = ? and nickname = ?",
                    new BeanListHandler<Orders>(Orders.class),
                    stateId, nickname);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    // 获取购物车数据的俄后半部分（商品信息表）
    public Orders_GoodsInfoVO getOrders_GoodsInfoVO(Integer goodsId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Orders_GoodsInfoVO query = null;
        try {
            query = runner.query("select id,img,name,price as unitPrice from goods where id = ?",
                    new BeanHandler<Orders_GoodsInfoVO>(Orders_GoodsInfoVO.class),
                    goodsId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    public List<Orders> getOrderInfoByNickName(String nickname) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Orders> query = null;
        try {
            query = runner.query("select * from orders where nickname = ?",
                    new BeanListHandler<Orders>(Orders.class),
                    nickname);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }
}
